package com.xxx.car.redisCluster.config;

import com.xxx.car.redisCluster.utils.RedisKey;
import com.xxx.car.redisCluster.utils.SerializeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * Created by youxiaojia on 2017/6/12.
 */

@Component
public class RedisTemplate {
    private static final Logger LOGGER = LoggerFactory.getLogger(RedisTemplate.class);

    //@Autowired
    //protected BinaryJedisCluster jedisCluster;

    @Autowired
    protected JedisPool jedisPool;

    @Autowired
    protected RedisProperties redisProperties;

    /**
     * 设置永不过期
     * @param redisKey 缓存的key对象
     * @param value  缓存value
     */
    public void set(RedisKey redisKey, Object value) {

        Jedis jedis = jedisPool.getResource();
        jedis.set(redisKey.getRedisKey().getBytes(), SerializeUtil.serialize(value));
        jedis.close();

        LOGGER.debug("RedisUtil:set cache key={},value={}", redisKey.getRedisKey(), value);
    }

    public void setnx(RedisKey redisKey, Object value){
        Jedis jedis = jedisPool.getResource();
        jedis.setnx(redisKey.getRedisKey().getBytes(), SerializeUtil.serialize(value));
        jedis.close();
    }
}
